AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create("sent_spaceanon_microasteroids")
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Initialize()
	self.Entity:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:MakeSwarm()
end

function ENT:MakeSwarm()
	local swarms = math.random(2,10)
	local AttackVector = Vector(math.random(-9999,9999),math.random(-9999,9999),math.random(-9999,9999)):Normalize()
	
	local basepoint = ents.Create("prop_dynamic") --since we can't do IsInWorld on a vector we'll make a marker; Spawn once, use may times.
	basepoint:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
	basepoint:SetPos(Vector(0,0,0))
	basepoint:Spawn()
	
	local trA = nil --localize now so we can access later
	local trB = nil
	
	for i=1, swarms do
		local useA = false
		while true do
			while true do
				basepoint:SetPos(VectorRand()*16384)
				if basepoint:IsInWorld() then
					break
				end
			end	
			trA = util.QuickTrace(basepoint:GetPos(),basepoint:GetPos()+(AttackVector*99999999))
			trB = util.QuickTrace(basepoint:GetPos(),basepoint:GetPos()+(AttackVector*-99999999))
			if trA.HitSky or trB.HitSky then
				if trA.HitSky then
					useA = true
				end
				break
			end
		end
		local bullet = {}
		bullet.Num 			= math.random(15,50)
		if not useA then
			bullet.Src		= trA.HitPos
		else
			bullet.Src		= trB.HitPos
		end
		bullet.Dir 			= AttackVector
		bullet.Spread 		= Vector(0.6,0.6,0.6)
		bullet.Tracer		= 1
		bullet.TracerName 	= "AirboatGunHeavyTracer"
		bullet.Force		= 2500
		bullet.Damage		= math.random(100,250)
		bullet.Attacker 	= self.Entity
		self.Entity:FireBullets(bullet)
	end
	basepoint:Remove()
	self.Entity:Remove()
end